import 'dart:convert';

import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_lang/lang_index.dart';
import 'package:route_manager/wx_navigator.dart';
import 'package:wallet_home/model/wallet_list_model.dart';
import 'package:wallet_home/module_config.dart';
import 'package:wallet_home/page/theme/colors.dart';
import 'package:wallet_home/search/model/search_model.dart';
import 'package:wallet_home/search/pages/custom_app_bar.dart';
import 'package:wallet_home/search/pages/search_bar.dart';
import 'package:wallet_home/tools/common.dart';
import 'package:wallet_home/tools/theme_utils.dart';
import 'package:wallet_home/widget/coins_manager.dart';

class CoinSelectPage extends StatefulWidget {
  @override
  createState() {
    return _CoinSelectPageState();
  }
}

class _CoinSelectPageState extends State<CoinSelectPage> {
  List<CoinsBean> coins = [];
  List<CoinsBean> showCoins = [];

  bool _isTransfer;

  @override
  void initState() {
    super.initState();

    dataInit();

//    List<SessionCoinModel> sessionCoin = CoinManager().allCoinList;
//
//    sessionCoin.forEach((element) {
//      coins.addAll(element.coins);
//    });
    coins.addAll(CoinManager().allSelectedCoinList);

    showCoins = [...coins];
  }

  dataInit() {
    String selectListData = SpUtil.getString(Constant.selectCoinListKey);

    if (selectListData != null && selectListData.isNotEmpty) {
      List jsonList = json.decode(selectListData);

      print("jsonList: $jsonList");
    }
  }

  @override
  Widget build(BuildContext context) {
    Map args = ModalRoute.of(context).settings.arguments;
    print("args: $args");
    if (args != null) _isTransfer = args[Constant.isTransfer];

    return Scaffold(
      appBar: CustomAppBar(
        context,
        child: GestureDetector(
          child: SearchBar(
            hideLeft: false,
            hideRight: true,
            hint: "${WXLang.$t("search_coins",package: ModuleConfig.moduleName)}",
            leftButtonClick: () {
             // WXNavigator.pushNamed(routeName: "/theme_page");
               Navigator.pop(context);
            },
            onChanged: (string) {
              if (string.isEmpty) {
                showCoins = [...coins];
              } else {
                showCoins.clear();
                coins.forEach((e) {
                  if (e.title.contains(string?.toUpperCase())) {
                    showCoins.add(e);
                  }
                });
              }

              setState(() {});
            },
          ),
        ),
      ),
      body: Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          color: ThemeUtils.getThemeColor(
              context: context,
              brightnessColor: Colors.white70,
              darkColor: Colors.black12),
          child: ListView.separated(
              itemBuilder: (ctx, index) =>
                  _buildDrawerListItem(showCoins[index]),
              separatorBuilder: (ctx, index) => Divider(
                    height: 0.5,
                    color: Colors.grey[200],
                  ),
              itemCount: showCoins?.length)),
    );
  }

  _buildDrawerListItem(CoinsBean bean) {
    return InkWell(
      onTap: () {
        String route = _isTransfer ? "/transfer_main" : "/collection_qr_code";
        WXNavigator.pushNamed(
            context: context, routeName: route, arguments: bean);
      },
      child: Container(
        height: 60,
        color: ThemeUtils.getThemeColor(
            context: context,
            brightnessColor: Colors.white,
            darkColor: Colours.dark_bg_color),
        width: double.infinity,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.only(left: 15),
              child: Image.asset(
                bean.img,
                width: 30,
                height: 30,
                fit: BoxFit.cover,
                package: ModuleConfig.moduleName,
              ),
            ),
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Container(
                  alignment: Alignment.center,
                  child: Text(
                    bean.title,
                    style: TextStyle(
                        color: ThemeUtils.getThemeColor(
                            context: context,
                            brightnessColor: Colors.grey[800],
                            darkColor: Colors.white70),
                        fontSize: 14),
                  ),
                )),
            Spacer(),
            Padding(
                padding: EdgeInsets.only(right: 15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      bean.num,
                      style: TextStyle(
                          color: ThemeUtils.getThemeColor(
                              context: context,
                              brightnessColor: Colors.grey[800],
                              darkColor: Colors.white70),
                          fontSize: 12),
                    ),
                    Text(
                      "${bean.cnyMoney} CNY",
                      style: TextStyle(
                          color: ThemeUtils.getThemeColor(
                              context: context,
                              brightnessColor: Colors.grey[800],
                              darkColor: Colors.white70),
                          fontSize: 10),
                    )
                  ],
                )),
          ],
        ),
      ),
    );
  }
}
